package com.kedacom.demo;

import java.io.File;
import java.nio.file.Paths;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author luocanfeng
 * @date 2011-6-20 10:10:14
 */
@Controller
public class UploadController {

	private static final String upload_folder = "/files";

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public void upload(HttpServletRequest request, HttpServletResponse response, String moid, MultipartFile file1,
			MultipartFile file2, MultipartFile file3, MultipartFile file4) throws Exception {
		String upload_file_path = request.getSession().getServletContext().getRealPath("/") + upload_folder;
		File uploadFolder = Paths.get(upload_file_path).toFile();
		File folder = new File(uploadFolder, "h/" + moid);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		System.out.println("上传文件存放路径：" + folder.getAbsolutePath());

		file1.transferTo(new File(folder, file1.getOriginalFilename()));
		file2.transferTo(new File(folder, file2.getOriginalFilename()));
		file3.transferTo(new File(folder, file3.getOriginalFilename()));
		file4.transferTo(new File(folder, file4.getOriginalFilename()));

		DirectlyRenderUtils.renderText(response, "upload success.");
	}

}
